//
//  YeeCarSelectedSuitVC.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/3/16.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeCarSelectedSuitVC.h"
#import "YeeShopDetailTipTableViewCell.h"

@interface YeeCarSelectedSuitVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView      *_tableView;
    UIButton         *_closeButton;
    UILabel          *_titleLable;
    UILabel          *_priceLable;

}
@end

@implementation YeeCarSelectedSuitVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)addNavbarView
{
    [super addNavbarView];
}
-(void)addOwnViews
{
    [super addOwnViews];
    [self addTableView];
    [self addSubsView];
}
-(void)configOwnViews
{
    [super configOwnViews];
    [self.view setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.4]];
}
-(void)bindViewModel
{
    [super bindViewModel];
}
-(void)addTableView
{
    
    _tableView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.tableFooterView=[[UIView alloc] init];
    [_tableView registerClass:[YeeShopDetailTipTableViewCell class] forCellReuseIdentifier:[YeeShopDetailTipTableViewCell cellIdentifier]];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make)
    {   make.left.right.equalTo(self.view);
        make.height.mas_equalTo(kMainScreenWidth-70);
        make.bottom.equalTo(self.view).with.offset(-40);
    }];
    
}
-(void)addSubsView
{

    UIView  *headerview=[[UIView alloc]init];
    [headerview setBackgroundColor:kWhiteColor];
    [self.view addSubview:headerview];
    [headerview mas_makeConstraints:^(MASConstraintMaker *make)
     {   make.left.right.equalTo(self.view);
         make.bottom.equalTo(_tableView.mas_top);
         make.height.mas_equalTo(70);
    }];

    UIImageView  *imageView=[[UIImageView alloc] init];
    imageView.contentMode=UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds=YES;
    [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://upload-images.jianshu.io/upload_images/1039119-68628a1a6aa65b63.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"] placeholderImage:[UIImage imageNamed:@""]];
     [headerview addSubview:imageView];
     [imageView mas_makeConstraints:^(MASConstraintMaker *make)
     {
        make.left.equalTo(headerview).with.offset(10);
        make.height.width.mas_equalTo(50);
        make.centerY.equalTo(headerview.mas_centerY);
     }];
    _priceLable=[[UILabel alloc] init];
    _priceLable.font=[UIFont Y_systemFontOfSize:14];
    _priceLable.textColor=kNavBarThemeColor;
    _priceLable.text=@"￥30";
    [headerview addSubview:_priceLable];
    [_priceLable mas_makeConstraints:^(MASConstraintMaker *make)
     {   make.left.equalTo(imageView.mas_right).with.offset(5);
         make.height.mas_equalTo(21);
         make.centerY.equalTo(headerview.mas_centerY);
    }];
    
    _closeButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_closeButton setTitle:@"确定" forState:UIControlStateNormal];
    [_closeButton setBackgroundColor:kNavBarThemeColor];
    [_closeButton setFrame:CGRectMake(0, kMainScreenHeight-40, kMainScreenWidth, 40)];
    [self.view addSubview:_closeButton];
    @weakify(self);
    [[_closeButton rac_signalForControlEvents:UIControlEventTouchUpInside ]subscribeNext:^(__kindof UIControl * _Nullable x)
     {
         @strongify(self);
         [self.view removeFromSuperview];
         [self removeFromParentViewController];
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YeeShopDetailTipTableViewCell *cell=[tableView  dequeueReusableCellWithIdentifier:[YeeShopDetailTipTableViewCell cellIdentifier] forIndexPath:indexPath];
    return cell;

    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //这里需要计算高度
    
    return [self configure];

}
-(CGFloat )configure
{
   
   NSMutableArray *listArray=[[NSMutableArray alloc] initWithArray:@[@"测试数据测试数据",@"云标测试数据签",@"历史记录",@"测试",@"妈妈说标题要长",@"测试数据",@"机智的手哥",@"妈妈说标题要长"]];
    CGFloat btnX = 10;
    CGFloat btnW = 0;
    CGFloat nextWidth = 0;  // 下一个标签的宽度
    CGFloat moreWidth = 0;  // 每一行多出来的宽度
    /**
     *  每一行的最后一个tag的索引的数组和每一行多出来的宽度的数组
     */
    NSMutableArray *lastIndexs = [NSMutableArray array];
    NSMutableArray *moreWidths = [NSMutableArray array];
    
    for (NSInteger i=0; i<listArray.count; i++)
    {
        
        btnW =[NSString caculateWidthWithFont:[UIFont Y_systemFontOfSize:14] text:listArray[i]]+20;
        if (i < listArray.count-1)
        {
            nextWidth =[NSString caculateWidthWithFont:[UIFont Y_systemFontOfSize:14] text:listArray[i]]+20+10;
        }
        CGFloat nextBtnX = btnX + btnW + 10;
        // 如果下一个按钮，标签最右边则换行
        if ((nextBtnX + nextWidth) > (kMainScreenWidth - 10))
        {   // 计算超过的宽度
            moreWidth = kMainScreenWidth - nextBtnX;
            [lastIndexs addObject:[NSNumber numberWithInteger:i]];
            [moreWidths addObject:[NSNumber numberWithFloat:moreWidth]];
            btnX = 10;
        }else
        {
            btnX += (btnW + 10);
        }
        // 如果是最后一个且数组中没有，则把最后一个加入数组
        if (i == listArray.count -1)
        {
            if (![lastIndexs containsObject:[NSNumber numberWithInteger:i]]) {
                [lastIndexs addObject:[NSNumber numberWithInteger:i]];
                [moreWidths addObject:[NSNumber numberWithFloat:0]];
              }
        }
    }
     return 40 * lastIndexs.count+25;
    
}
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text sizeWithAttributes:attrs];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
